<?php

class Specialty extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('specialty_model' , 'SPM');
	}
	
	function index()
	{
		//get all specialty(s) to display them
		$result = $this->SPM->get_specialty();
		
		//if $result['count'] = 0 then there is no data
		//else generate array for all types
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['result'] = $result['info'];
		}
		
		$this->load->view('head');
		$this->load->view('specialty/specialty_main', $data);
		$this->load->view('footer');
	}
	
	
	function new_specialty($id='')
	{
	
		if($id!=''){
			$data['specialtyid']	=	$id;
			$result = $this->SPM->get_specialty($id);
			//print_r($result);
			$data['specialty']	= $result['name'];
			
			$data['action']		=	site_url().'/specialty/new_specialty2/'.$id;
		}
		else
			$data['action']	=	site_url().'/specialty/new_specialty2';
		$this->load->view('head');
		$this->load->view('specialty/specialty_form' , $data);
		$this->load->view('footer');
	}
	
	function new_specialty2($id='')
	{

		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if ($this->form_validation->run('specialty') == FALSE)
		{			
			if($id!='')
				$data['action']	=	site_url().'/specialty/new_specialty2/'.$id;
			else
				$data['action']	=	site_url().'/specialty/new_specialty2';
			
			$this->load->view('head');
			$this->load->view('specialty/specialty_form' , $data);
			$this->load->view('footer');
		}
		else
		{
			$data = array(
				'id' 			=> $this->input->post('specialtyid'),
				'name' 			=> $this->input->post('specialty')
			);
			
			//Edit:
			if(isset($id) && $id!=''){
				if($this->SPM->update_specialty_by_id($data, $id))
				redirect('specialty/' , 'refresh');
			}
			else{
				if($this->SPM->insert_new_specialty($data))
					redirect('specialty/' , 'refresh');
			}
		}
	}
	
	function delete_specialty($id)
	{
		if(isset($id) && $id!=''){
			//check if there is a user connect to this specialty before the delete
				if($this->SPM->delete_specialty_by_id($id)){
					redirect('specialty/' , 'refresh');
				}
				else
				{
					echo "<script>alert('".lang('msg_delete_specialty_error')."');</script>";
					$this->index();
				}
		}
	}
	
}

/* End of file specialty.php */
/* Location: ./system/application/controllers/specialty.php */
